#ifndef KCPSERVER_H
#define KCPSERVER_H

#include <queue>
#include "kcppacket.h"
#include <netinet/in.h>
#include <map>
#include "udpclient.h"
#include <string>
#include "kcpagent.h"

using namespace std;

class KCPServer {
public:
	static KCPServer &GetInstance()
	{
		static KCPServer instance;
		return instance;
	}

	void RemoveAgent(KCPAgent *agent);
	void OnReceiveUDP(string ip, string port, shared_ptr<std::vector<unsigned char> > data);
	void Update();
	void SendUDPPacket(shared_ptr<KCPPacket> packet);
	void ReceiveKCPPacket(shared_ptr<KCPPacket> packet);
	void SendData(string ip, string port, shared_ptr<std::vector<unsigned char> > data);
	std::vector<shared_ptr<KCPPacket> > *GetSendUDPPackets(){
		return &sendUDPPackets;
	}
	std::vector<shared_ptr<KCPPacket> > *GetReceiveKcpPackets() {
		return &receiveKcpPackets;
	}
	std::vector<shared_ptr<KCPAgent> > *GetClosedAgents() {
		return &closedAgents;
	}

	//业务逻辑层关闭
	void CloseAgent(string udpPort);

private:
	map<string, shared_ptr< KCPAgent> > agents;
	void AddAgent(shared_ptr<KCPAgent> agent);
	double lastTime = 0;
	//返回给业务层的数据
	std::vector<shared_ptr<KCPPacket> > sendUDPPackets;
	std::vector<shared_ptr<KCPPacket> > receiveKcpPackets;
	std::vector<shared_ptr<KCPAgent> > closedAgents;
	/*
	shared_ptr<UDPClient> udpclient;
	queue<shared_ptr< KCPPacket> > packets;
	
	void DequeueMsg();
	void OnSend();
	*/
};
#endif

